﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TicketingSystem;
using System.Data.Entity;

namespace TicketingSystemAsp
{
    public partial class Events : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                BindListView();
            }
        }
  
        private void BindListView()
        {
            this.EventsListView.DataSource = TicketingEnchancements.GetAllEvents();
            this.DataBind();
        }

        protected void EditEvent(object sender, ListViewEditEventArgs args)
        {
            this.EventsListView.EditIndex = args.NewEditIndex;
            BindListView();

            DropDownList dropDownList= EventsListView.EditItem.FindControl("CategoriesDropDownList") as DropDownList;
            //DropDownList dropDownList = this.Dropd;

            dropDownList.DataSource = TicketingEnchancements.GetCategories();
            dropDownList.DataValueField = "CategoryID";
            dropDownList.DataTextField = "CategoryName";
            dropDownList.SelectedValue = (EventsListView.Items[args.NewEditIndex].FindControl("HiddenCategoryIdLabel") as Label).Text;

            dropDownList.DataBind();

            DropDownList dropDownListLocations = EventsListView.EditItem.FindControl("LocationDropDownList") as DropDownList;

            dropDownListLocations.DataSource = TicketingEnchancements.GetLocations();
            dropDownListLocations.DataValueField = "LocationID";
            dropDownListLocations.DataTextField = "LocationName";
            dropDownListLocations.SelectedValue = (EventsListView.Items[args.NewEditIndex].FindControl("HiddenLocationIdLabel") as Label).Text;

            dropDownListLocations.DataBind();

            Calendar calendar = EventsListView.EditItem.FindControl("Calendar") as Calendar;
            int id = int.Parse((EventsListView.EditItem.FindControl("EventIDLabel") as Label).Text);
            calendar.SelectedDate = TicketingEnchancements.GetEventByID(id).EventTime;

        }

        protected void UpdateEvent(object sender, ListViewUpdateEventArgs args)
        {
            string title = (EventsListView.Items[args.ItemIndex].FindControl("TitleTextBox") as TextBox).Text;
            int id = int.Parse((EventsListView.Items[args.ItemIndex].FindControl("EventIDLabel") as Label).Text);
            DateTime time = (EventsListView.Items[args.ItemIndex].FindControl("Calendar") as Calendar).SelectedDate;
            int categoryID = int.Parse((EventsListView.Items[args.ItemIndex].FindControl("CategoriesDropDownList")
                as DropDownList).SelectedValue);
            int locationID = int.Parse((EventsListView.Items[args.ItemIndex].FindControl("LocationDropDownList")
                as DropDownList).SelectedValue);
            decimal price = decimal.Parse((EventsListView.Items[args.ItemIndex].FindControl("PriceLabel") as TextBox).Text);

            TicketingEnchancements.UpdateEvent(id, new Event() 
            {
                EventTitle = title,
                EventTime = time, 
                CategoryID = categoryID, 
                LocationID = locationID,
                Price = price
            });

            EventsListView.EditIndex = -1;
            BindListView();
        }

        protected void DeleteEvent(object sender, ListViewDeleteEventArgs args)
        {
            int id = int.Parse((EventsListView.Items[args.ItemIndex].FindControl("IDLabel1") as Label).Text);

            TicketingEnchancements.DeleteEvent(id);

            BindListView();
        }

        protected void AddEvent(object sender, ListViewInsertEventArgs args)
        {
            try
            {
                string title = (EventsListView.InsertItem.FindControl("NewTitleTextBox") as TextBox).Text;
                DateTime time = (EventsListView.InsertItem.FindControl("NewTimeCalendar") as Calendar).SelectedDate;
                int locationId = int.Parse((EventsListView.InsertItem.FindControl("NewLocation") as DropDownList).SelectedValue);
                int categoryId = int.Parse((EventsListView.InsertItem.FindControl("NewCategory") as DropDownList).SelectedValue);
                decimal price = decimal.Parse((EventsListView.InsertItem.FindControl("NewPrice") as TextBox).Text);

                TicketingEnchancements.AddEvent(new Event()
                {
                    EventTitle = title,
                    EventTime = time,
                    LocationID = locationId,
                    CategoryID = categoryId,
                    Price = price
                });
            }
            catch
            {
                return;
            }
            
            EventsListView.InsertItemPosition = InsertItemPosition.None;

            BindListView();
        }

        protected void ShowInsertTemplate(object sender, CommandEventArgs args)
        {
            EventsListView.InsertItemPosition = InsertItemPosition.LastItem;
            
            BindListView();

            DropDownList categories =  EventsListView.InsertItem.FindControl("NewCategory") as DropDownList;
            categories.DataSource = TicketingEnchancements.GetCategories();
            categories.DataValueField = "CategoryID";
            categories.DataTextField = "CategoryName";
            categories.DataBind();

            DropDownList locations = EventsListView.InsertItem.FindControl("NewLocation") as DropDownList;
            locations.DataSource = TicketingEnchancements.GetLocations();
            locations.DataValueField = "LocationID";
            locations.DataTextField = "LocationName";
            locations.DataBind();
        }
    }
}